/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import junit.framework.TestCase;





/**
 * Performs Validation Test for url validations.
 *
 * @version $Revision: 1128446 $ $Date: 2011-05-27 13:29:27 -0700 (Fri, 27 May 2011) $
 */
public class UrlValidatorTest extends TestCase {

   private boolean printStatus = false;
   private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.

   public UrlValidatorTest(String testName) {
      super(testName);
   }

   
   
   public void testManualTest()
   {
	   UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_LOCAL_URLS);
     System.out.println("Manual Test");
	   System.out.println(urlVal.isValid("http://www.amazon.com") + " expected: true");
     System.out.println(urlVal.isValid("http://www.amazon.com:24535") + " expected: true");
     System.out.println(urlVal.isValid("http://www.amazon.com3") + " expected: false");
     System.out.println(urlVal.isValid("http3://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("ht2tp://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("http://www.amazon") + " expected: false");
     System.out.println(urlVal.isValid("http://amazon") + " expected: false");
     System.out.println(urlVal.isValid("amazon") + " expected: false");
     System.out.println(urlVal.isValid("http://...localhost") + " expected: true");
     System.out.println(urlVal.isValid("http://localhost:8080") + " expected: true");
     System.out.println(urlVal.isValid("http://192.168.56.1") + " expected: true");
     System.out.println(urlVal.isValid("http://265.234.231.1") + " expected: false");
	   
	   
   }
   
   
   public void testYourFirstPartition()
   {
    //Testing Schemes. I have limited it to just ftp:, http:
    // I am also not allowing all schemes.
	   UrlValidator urlVal = new UrlValidator();
     System.out.println("");
     System.out.println("First Parition");

     System.out.println(urlVal.isValid("http://www.amazon.com") + " expected: true");
     System.out.println(urlVal.isValid("3ht2tp1://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("http3://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("ht2tp://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("444http33://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("httpp://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("ftp://www.amazon.com") + " expected: true");
     System.out.println(urlVal.isValid("ftp3://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("3ftp://www.amazon.com") + " expected: false");
     System.out.println(urlVal.isValid("http://192.168.56.1") + " expected: true");
     System.out.println(urlVal.isValid("ftp://192.168.56.1") + " expected: true");
     System.out.println(urlVal.isValid("http1://192.168.56.1") + " expected: false");
     System.out.println(urlVal.isValid("1http://192.168.56.1") + " expected: false");
     System.out.println(urlVal.isValid("192.168.56.1") + " expected: false");
     System.out.println(urlVal.isValid("ftp1://192.168.56.1") + " expected: false");
     System.out.println(urlVal.isValid("http://265.234.231.1") + " expected: false");

   }
   
   public void testYourSecondPartition(){
	   //Testing paths.
    // I am also not allowing local urls.
     UrlValidator urlVal = new UrlValidator();
     System.out.println("");
     System.out.println("Second Parition");

     System.out.println(urlVal.isValid("http://www.amazon.com/hey/how/are/you.html") + " expected: true");
     System.out.println(urlVal.isValid("http://www.amazon.com/////") + " expected: false");
     System.out.println(urlVal.isValid("http://www.amazon.com/../../hey") + " expected: false");
     System.out.println(urlVal.isValid("http://www.amazon.com/../") + " expected: false");
     System.out.println(urlVal.isValid("http://www.amazon.com//") + " expected: false");
     System.out.println(urlVal.isValid("http://www.amazon.com/..") + " expected: false");
     System.out.println(urlVal.isValid("ftp://www.amazon.com/hey") + " expected: true");
     System.out.println(urlVal.isValid("ftp://www.amazon.com/?/") + " expected: false");
     System.out.println(urlVal.isValid("ftp://www.amazon.com/....../") + " expected: false");
     System.out.println(urlVal.isValid("http://192.168.56.1/hey/ha?hey=5") + " expected: true");
     System.out.println(urlVal.isValid("ftp://192.168.56.1/hey/how/are/you.html") + " expected: true");
     System.out.println(urlVal.isValid("http://192.168.56.1..//.") + " expected: false");
     System.out.println(urlVal.isValid("http://192.168.56.1/...../") + " expected: false");
     System.out.println(urlVal.isValid("ftp://192.168.56.1/1234/2395...34905.29424/") + " expected: false");
     System.out.println(urlVal.isValid("ftp://192.168.56.1:80/hey/") + " expected: true");
     System.out.println(urlVal.isValid("http://265.234.231.1") + " expected: true");

     //System.out.println(testUrlScheme[])
   }
   
   
   public void testIsValid()
   {
    int count = 0;
    UrlValidator urlVal = new UrlValidator(null, null, UrlValidator.ALLOW_LOCAL_URLS);
    Boolean test_valid = true;
	   for(int i = 0;i<testUrlScheme.length;i++){
      String test_url = testUrlScheme[i].item;
      if(testUrlScheme[i].valid == false){
        test_valid = false;
      }

		   for(int p = 0; p < testUrlAuthority.length; p++){

        String test_url_authority = testUrlAuthority[p].item;
        if(testUrlAuthority[p].valid == false){
          test_valid = false;
        }

        for(int r = 0; r < testUrlPort.length; r++){

          String test_url_port = testUrlPort[r].item;
          if(testUrlPort[r].valid == false){
            test_valid = false;
          }

          for(int t = 0; t < testPath.length; t++){

            String test_path = testPath[t].item;

            String test_string = test_url + test_url_authority + test_url_port + test_path;
            if(testPath[t].valid == false){
              test_valid = false;
            }
            count += 1;
            boolean result = urlVal.isValid(test_string);
            if(test_valid == false){
              assertFalse(test_string + "should not validate true", urlVal.isValid(test_string));
            }
            else{
              assertTrue(test_string + "should not validate false", urlVal.isValid(test_string));
            }
            System.out.println(test_string + " is testing as: "+ urlVal.isValid(test_string) + " expected to be "+ test_valid);

          }

        }
       }
	   }
     System.out.println(count);
   }
   
   public void testAnyOtherUnitTest()
   {
	   
   }

   ResultPair[] testUrlScheme = {new ResultPair("http://", true),
                               new ResultPair("ftp://", true),
                               new ResultPair("h3t://", true),
                               new ResultPair("3ht://", false),
                               new ResultPair("http:/", false),
                               new ResultPair("http:", false),
                               new ResultPair("http/", false),
                               new ResultPair("://", false),
                               new ResultPair("", true)};

  ResultPair[] testUrlAuthority = {new ResultPair("www.amazon.com", true),
                                  new ResultPair("0.0.0.0", true),
                                  new ResultPair("255.255.255.255", true),
                                  new ResultPair("256.256.256.256", false),
                                  new ResultPair("1.2.3.4.5", false),
                                  new ResultPair("1.2.3.4.", false),
                                  new ResultPair("1.2.3", false),
                                  new ResultPair(".1.2.3.4", false),
                                  new ResultPair("aaa.", false),
                                  new ResultPair(".aaa", false),
                                  new ResultPair("aaa", false),
                                  new ResultPair("", false)};

  ResultPair[] testUrlPort = {new ResultPair(":80", true),
                             new ResultPair(":65535", true),
                             new ResultPair(":6553", true),
                             new ResultPair(":655", true),
                             new ResultPair(":65", true),
                             new ResultPair(":6", true),
                             new ResultPair(":0", true),
                             new ResultPair("", true),
                             new ResultPair(":-1", false),
                             new ResultPair(":65636", true),
                             new ResultPair(":65a", false)};

  ResultPair[] testPath = {new ResultPair("/test1", true),
                          new ResultPair("/t123", true),
                          new ResultPair("/$23", true),
                          new ResultPair("/..", false),
                          new ResultPair("/../", false),
                          new ResultPair("/test1/", true),
                          new ResultPair("", true),
                          new ResultPair("/test1/file", true),
                          new ResultPair("/..//file", false),
                          new ResultPair("/test1//file", false)};
   /**
    * Create set of tests by taking the testUrlXXX arrays and
    * running through all possible permutations of their combinations.
    *
    * @param testObjects Used to create a url.
    */
   

}
